﻿using Dapper;
using HR.Filters;
using HR.Hubs;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System.Data.SqlClient;

namespace HR.Controllers
{
    public class ZhiWeiFaBuDengJiController : Controller
    {
        private readonly IZhiWeiFaBuDengJiServices zhiWeiFaBuDengJiServices;
        private readonly IHubContext<InfoHub> hubContext;
        [TypeFilter(typeof(RoleFilterAttribute))]
        public IActionResult Index()
        {
            return View();
        }
        public ZhiWeiFaBuDengJiController(IZhiWeiFaBuDengJiServices zhiWeiFaBuDengJiServices,IHubContext<InfoHub> hubContext)
        {
            this.zhiWeiFaBuDengJiServices = zhiWeiFaBuDengJiServices;
            this.hubContext = hubContext;
        }
        //依赖注入标题
        [TypeFilter(typeof(SignalRFilterAttribute),Arguments =new object[] {"职位登记"})]
        //职位发布登记
        [HttpPost]
        public async Task<IActionResult> Insert([FromBody]ZhiWeiFaBuDengJi zhiWeiFaBuDengJi) 
        {
            int result = await zhiWeiFaBuDengJiServices.Add(zhiWeiFaBuDengJi);
            if (result > 0)
            {
                //发送消息
                //await hubContext.Clients.All.SendAsync("CheckInfo","职位登记", "新的职位登记信息可变更！");
                return Content(result.ToString());
            }
            else 
            {
                return View(zhiWeiFaBuDengJi);
            }
        }
        //职位分类登记  添加  一二三级下拉框合并
        public IActionResult HeBing()
        {
            //获取一级数据
            string sql = @"select first_kind_id as id,first_kind_name as name from config_file_first_kind";
            SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=HR_DB;Integrated Security=True");
            List<DFirstKind> list = con.Query<DFirstKind>(sql).ToList();
            foreach (DFirstKind item in list)
            {
                //获取二级数据
                string sql2 = $@"select second_kind_id as id,second_kind_name as name from config_file_second_kind where first_kind_id='{item.id}'";
                item.children = con.Query<DSecondkind>(sql2).ToList();
                foreach (DSecondkind th in item.children) 
                {
                    //获取三级数据
                    string sql3 = $@"select third_kind_id as id,third_kind_name as name from dbo.config_file_third_kind where second_kind_id='{th.id}'";
                    th.children = con.Query<DThreeKind>(sql3).ToList();
                }
            }
            return Json(list);
        }
        //查询职位分类
        [HttpGet]
        public async Task<IActionResult> ZhiWeiFenLei() 
        {
            List<ZhiWeiFenLei> list = await zhiWeiFaBuDengJiServices.ZhiWeiFenLei();
            return Json(list);
        }
        //查询职位名称
        [HttpGet]
        public async Task<IActionResult> ZhiWeiName() 
        {
            List<ZhiWei> list = await zhiWeiFaBuDengJiServices.ZhiWeiName();
            return Json(list);
        }
        //职位发布分页
        [HttpGet]
        public async Task<IActionResult> FenYe(int size,int currentPage) 
        {
            FenYe<ZhiWeiFaBuDengJi> list = await zhiWeiFaBuDengJiServices.FenYe(size, currentPage);
            return Json(list);
        }
        [TypeFilter(typeof(RoleFilterAttribute))]
        //职位发布变更
        public IActionResult BianGen() 
        {
            return View();
        }
        //职位发布变更查询
        [HttpGet]
        public async Task<IActionResult> ZhiWeiFaBuFindAll() 
        {
            List<ZhiWeiFaBuDengJi> list = await zhiWeiFaBuDengJiServices.FindAll();
            return Json(list);
        }
        //职位发布修改
        [HttpPut]
        public async Task<IActionResult> XiuGai([FromBody] ZhiWeiFaBuDengJi zhiWeiFaBuDengJi) 
        {
            int result = await zhiWeiFaBuDengJiServices.Update(zhiWeiFaBuDengJi);
            if (result > 0)
            {
                return Content(result.ToString());
            }
            else 
            {
                return View(result);
            }
        }
        //职位发布删除
        [HttpGet]
        public async Task<IActionResult> ShanChu(int mre_id) 
        {
            int result = await zhiWeiFaBuDengJiServices.Delete(mre_id);
            if (result > 0)
            {
                return Content(result.ToString());
            }
            else 
            {
                return View(mre_id);
            }
        }
        [TypeFilter(typeof(RoleFilterAttribute))]
        //职位发布查询
        public IActionResult ChaXun() 
        {
            return View();
        }
    }
}
